/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.hiep.connect.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.hiep.connect.client.UserRpcService;
import com.hiep.connect.server.dao.MyUserDao;
import com.hiep.connect.server.dao.MyUserTwigDao;
import com.hiep.connect.shared.MyUser;
import com.hiep.connect.shared.Posting;
import com.hiep.connect.shared.PostingThread;

public class UserRpcServiceImpl extends RemoteServiceServlet implements
		UserRpcService {

	private static final long serialVersionUID = 201204231L;

	// private MyUserDao userDao;

	private MyUserTwigDao twigDao;

	public UserRpcServiceImpl() {
		// userDao = new MyUserDao();
		twigDao = new MyUserTwigDao();
	}

	@Override
	public MyUser login(String email, String password)
			throws IllegalArgumentException {
		// TODO Auto-generated method stub
		MyUser currentUser = new MyUser();
		currentUser.setEmail(email);
		currentUser.setPassword(password);

		// MyUser find = userDao.find(currentUser);
		// MyUser generatedUser = fillUser(find);

		List<MyUser> all = twigDao.getAll();
		for (MyUser u : all) {
			if (u.getEmail().equalsIgnoreCase(email)) {
				return u;
			}
		}
		return null;
	}

	@Override
	public MyUser login(MyUser user) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public MyUser login(String requestUri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void logout(MyUser user) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		MyUser find = twigDao.find(user);
		twigDao.update(find);
	}

	@Override
	public MyUser register(String email, String password)
			throws IllegalArgumentException {
		// TODO Auto-generated method stub
		MyUser newUser = new MyUser();
		newUser.setEmail(email);
		newUser.setPassword(password);

		MyUser generatedUser = fillUser(newUser);
		twigDao.save(generatedUser);

		// userDao.save(newUser);
		// MyUser find = userDao.find(newUser);

		// MyUser generatedUser = fillUser(newUser);
		// userDao.save(generatedUser);
		// MyUser find = userDao.find(generatedUser);

		return generatedUser;
	}

	private MyUser fillUser(MyUser user) {
		List<MyUser> friends = new ArrayList<MyUser>();
		List<Posting> postings = new ArrayList<Posting>();

		for (int i = 1; i <= 5; i++) {
			// Add friends.

			String friendEmail = "friend " + i;
			MyUser u = new MyUser(friendEmail);
			friends.add(u);

			// Create a posting thread.
			Posting p = new Posting(friendEmail, "Posting content" + i,
					new Date());
			postings.add(p);
		}

		PostingThread thread = new PostingThread(postings);
		List<PostingThread> postingThreads = new ArrayList<PostingThread>();
		postingThreads.add(thread);

		user.setFriends(friends);
		user.setPostingThreads(postingThreads);

		return user;
	}

	@Override
	public List<MyUser> getAll() throws IllegalArgumentException {
		// TODO Auto-generated method stub
		// return userDao.getAll();
		return twigDao.getAll();
	}

	@Override
	public void update(MyUser user) throws IllegalArgumentException {
		// twigDao.update(user);
		logout(user);
	}

	@Override
	public List<MyUser> searchFor(String searchFor)
			throws IllegalArgumentException {
		List<MyUser> result = new ArrayList<MyUser>();
		List<MyUser> all = getAll();
		for (MyUser u : all) {
			String name = u.getName();
			String email = u.getEmail();
			String lowerCaseSearch = searchFor.toLowerCase();
			if ((name != null && name.toLowerCase().contains(lowerCaseSearch))
					|| (email != null && email.toLowerCase().contains(
							lowerCaseSearch))) {
				result.add(u);
			}
		}
		if (result.isEmpty()) {
			result = all;
		}

		return result;
	}

	@Override
	public void addPendingFriend(MyUser pendingFriend, List<MyUser> targets)
			throws IllegalArgumentException {
		for (MyUser target : targets) {
			MyUser find = twigDao.find(target);
			find.addToPendingFriendList(pendingFriend);
			twigDao.update(find);
		}

	}

	@Override
	public void postOnFriend(MyUser source, MyUser target, PostingThread thread)
			throws IllegalArgumentException {
		// TODO Auto-generated method stub
		MyUser find = twigDao.find(target);
		find.addPostingThread(thread);
		
		twigDao.save(find);
	}

	@Override
	public void acceptFriends(MyUser user, List<MyUser> newFriends)
			throws IllegalArgumentException {
		// TODO Auto-generated method stub

		MyUser find = twigDao.find(user);
		List<MyUser> pendingFriends = find.getPendingFriends();
		if (pendingFriends != null && pendingFriends.size() > 0) {
			find.addAllFriends(pendingFriends);
			find.setPendingFriends(null);
		}

		twigDao.update(find);
	}

}
